<template>
  <div class="content">
    {{ 'xlsx' }}
    <a-button type="primary" @click="getFile">导出</a-button>
  </div>
</template>

<script setup lang="ts">
import * as XLSX from 'xlsx';
import { ref } from 'vue';
import dayjs from 'dayjs';
//  const headers = [{CT:'定位',LA:'治疗',MODEL:'制模',}]
const arr = ['时间', '定位', '治疗', '制模', '计划'];
const filterIndex = ['date', 'CT', 'LA', 'MODEL', 'PLAN_DESIGN'];
const data1 = [
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 12, date: '2023-01-02' },
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 13, date: '2023-01-03' },
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 14, date: '2023-01-04' },
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 15, date: '2023-01-05' },
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 16, date: '2023-01-06' },
  { CT: 0, LA: 0, MODEL: 1, PLAN_DESIGN: 17, date: '2023-01-07' }
];
const filterIndex2 = ['日期', '体膜', '发泡胶', '头膜', '塑型垫'];

const data2 = [
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 12, date: '2023-01-02' },
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 13, date: '2023-01-03' },
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 14, date: '2023-01-04' },
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 15, date: '2023-01-05' },
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 16, date: '2023-01-06' },
  { 体膜: 0, 发泡胶: 0, 头膜: 1, 塑型垫: 17, date: '2023-01-07' }
];
const flag = ref<boolean>(true);
function exportExcel(flag: boolean, filename: string, data: any) {
  let exc = XLSX.utils.book_new();
  // const sheetData = data.map((item)=>({
  //   "日期":item.date,
  //   "定位":item.CT,
  //   "治疗":item.LA,
  //   "制模":item.MODEL,
  //   "计划":item.PLAN_DESIGN,
  // }))
  // console.log('sheetData: ', sheetData);

  const arr2: any = [];
  const dataIndex = Object.keys(data[0]).sort();
  console.log('dataIndex: ', dataIndex.sort());

  data.forEach((item) => {
    const obj2: any = {};
    dataIndex.forEach((t) => {
      if (item[t] !== undefined) {
        obj2[t] = item[t];
      }
    });
    arr2.push(obj2);
  });
  console.log('arr2: ', arr2);

  if (flag) return;
  const sheet1 = XLSX.utils.json_to_sheet(arr2);
  XLSX.utils.book_append_sheet(exc, sheet1, filename);
  XLSX.writeFile(exc, filename + '.xlsx');
}
const getFile = () => {
  // exportExcel('excel文件',exc_data)
  exportExcel(false, `excel文件-${dayjs().format('YYYY-MM-DD HH:mm')}`, data2);
};
</script>

<style lang="less" scoped></style>
